/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 6/12/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;        
namespace DataAccessLayer{
      /// <summary>
    /// Define LanguageDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in LanguageDao class , not here.
    /// </summary>
    public class LanguageDaoBase {
    private static ILog logger = null;
            #region Constructor
             /// <summary>
            /// Define Constructor.
             /// </summary>
            public LanguageDaoBase(){}
             static LanguageDaoBase()
            {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(LanguageDaoBase).FullName);
            }
 #endregion
              /// <summary>
             /// Read data from IDataReader, convert to Language Object.
            /// </summary>
            private static Language GetLanguageByDataReader(IDataReader reader)
    {
      String methodName = "GetLanguageByDataReader";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
        try
      {
        Language result = new Language();
              //read LanguageId field data.
             
            result.LanguageId=ManagerDao.GetstringDataFromDataReader(reader,"LanguageId");
            
              //read Name field data.
             
            result.Name=ManagerDao.GetstringDataFromDataReader(reader,"Name");
            
              //read Published field data.
             
            result.Published=ManagerDao.GetstringDataFromDataReader(reader,"Published");
            
              //read Order field data.
             
            result.Order=ManagerDao.GetintDataFromDataReader(reader,"Order");
            
              //read CreatedBy field data.
             
            result.CreatedBy=ManagerDao.GetstringDataFromDataReader(reader,"CreatedBy");
            
              //read CreatedDate field data.
             
            result.CreatedDate=ManagerDao.GetdatetimeDataFromDataReader(reader,"CreatedDate");
            
              //read LastUpdatedBy field data.
             
            result.LastUpdatedBy=ManagerDao.GetstringDataFromDataReader(reader,"LastUpdatedBy");
            
              //read LastUpdatedDate field data.
             
            result.LastUpdatedDate=ManagerDao.GetdatetimeDataFromDataReader(reader,"LastUpdatedDate");
            
             return result;
      }
      catch (Exception e)
      {
        // write error to log file.
        logger.Error(methodName, e);
        return null;
      }
    }
              /// <summary>
        /// Define function Insert for Language Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool Insert(Language obj)
            {
                bool result = false;
              String methodName = "InsertLanguage";
              if (logger.IsDebugEnabled)
              {
                LoggingUtility.logMethodEntry(logger, methodName);
              }
              DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertLanguage");            
            
                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(cmd, "LanguageId", DbType.AnsiString,obj.LanguageId);
                 //Add value "Name"  paramater.
				ManagerDao.AddInParameter(cmd, "Name", DbType.String,
					ConvertValueObject.CastObjValueToInsertDB(obj.Name));
                 //Add value "Published"  paramater.
				ManagerDao.AddInParameter(cmd, "Published", DbType.AnsiString,
					ConvertValueObject.CastObjValueToInsertDB(obj.Published));
                 //Add value "Order"  paramater.
				ManagerDao.AddInParameter(cmd, "Order", DbType.Int32,
					ConvertValueObject.CastObjValueToInsertDB(obj.Order));
                 //Add value "CreatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,
					ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
                 //Add value "CreatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,
					ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
                 //Add value "LastUpdatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,
					ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
                 //Add value "LastUpdatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,
					ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));    
            //add paramater store return value.        
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
              {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id>0)//Successfull.
                {
                  result = true;
                }
                else//Error.
                {
                  result = false;
                }
              }
              catch (Exception e)
              {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
              }
              return result;
            }
             
               /// <summary>
        /// Define function Update for Language Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool Update(Language obj)
          {
          bool result = false;
          String methodName = "UpdateLanguage";
          if (logger.IsDebugEnabled)
          {
            LoggingUtility.logMethodEntry(logger, methodName);
          }
             DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateLanguage");
           

                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(cmd, "LanguageId", DbType.AnsiString,ConvertValueObject.CastObjValueToInsertDB(obj.LanguageId));
                 //Add value "Name"  paramater.
				ManagerDao.AddInParameter(cmd, "Name", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.Name));
                 //Add value "Published"  paramater.
				ManagerDao.AddInParameter(cmd, "Published", DbType.AnsiString,ConvertValueObject.CastObjValueToInsertDB(obj.Published));
                 //Add value "Order"  paramater.
				ManagerDao.AddInParameter(cmd, "Order", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.Order));
                 //Add value "CreatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
                 //Add value "CreatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
                 //Add value "LastUpdatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
                 //Add value "LastUpdatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));           
            // add parameter store return value.
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
           try
          {
            ManagerDao.ExecuteNonQuery(cmd);
            int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
            if (rows == 1)//Successfull.
            {
              result = true;
            }
            else//Error.
            {
              result = false;
            }

          }
          catch (Exception e)
          {
            //write error Message to log file.
            logger.Error(methodName, e);
            result = false;
          }
          return result;
                }
/// <summary>
/// Get Data Language object. 
/// </summary>
/// <param name="languageId" >This is primary key to get data.</param>
/// <returns>Language object.</returns>
              
           public static Language Get(string languageId)
    {
      String methodName = "GetLanguage";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
      try
      {
        Language result = null;
        DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetLanguage");
                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(dbCommnand, "LanguageId", DbType.AnsiString,languageId);
        IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
        if (reader.Read())
        {
          result= GetLanguageByDataReader(reader);

        }
        reader.Close();
        return result;
      }
      catch (System.Exception e)
      {
        //write error message to log file.
        logger.Error("Get", e);
        return null;
      }
    }
              /// <summary>
        /// Get All Language object from table Language.
        /// </summary>
        /// <returns>List Language object.</returns>
             public static List<Language> GetAll()
                {
                 String methodName = "GetAll";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        List<Language> listLanguage = new List<Language>();
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllLanguage");

        using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
        {
          while (reader.Read())
          {
            listLanguage.Add(GetLanguageByDataReader(reader));
          }
          reader.Close();
        }

        return listLanguage;
      }
      catch (Exception ex)
      {
        //write error message to log file.
        logger.Error(methodName, ex);
        return null;
      }
    }

             /// <summary>
        /// Search Language object in table Language with condition is t object.
        /// </summary>
        /// <param name="t">Condition for Search.</param>       
        /// <param name="totalRow">  total records in this table.</param>
        /// <returns>List Language object.</returns>
             public static List<Language> SearchLanguage(Language t)
                {
                 String methodName = "SearchLanguage";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        List<Language> listLanguage = new List<Language>();
        DbCommand cmd = ManagerDao.GetStoredProcCommand("spSearchLanguage");
              //Add value Condition  paramater.
                ManagerDao.AddInParameter(cmd, "Condition", DbType.String,ConvertValueObject.CastObjValueToInsertDB(t.Condition));

                 //Add value PageIndex  paramater.
                ManagerDao.AddInParameter(cmd, "PageIndex", DbType.Int32, t.ObjectPaging.PageIndex);
                //Add value PageSize  paramater.
                ManagerDao.AddInParameter(cmd, "PageSize", DbType.Int32,  t.ObjectPaging.PageSize);
         //Add value SortField  paramater.
                ManagerDao.AddInParameter(cmd, "SortField", DbType.String, t.ObjectSortOrder.SortField);
                //Add value SortExpression  paramater.
                ManagerDao.AddInParameter(cmd, "SortExpression", DbType.String,  t.ObjectSortOrder.SortExpression);
          using (IDataReader reader = ManagerDao.ExecuteReader(cmd))
        {
          while (reader.Read())
          {
            listLanguage.Add(GetLanguageByDataReader(reader));
            if(t.ToltalRow<=0)
                 t.ToltalRow = ManagerDao.Getint32DataFromDataReader(reader, "TotalRow");
          }
         
          reader.Close();
        }

        return listLanguage;
      }
      catch (Exception ex)
      {
        //write error message to log file.
        logger.Error(methodName, ex);
        return null;
      }
    }
/// <summary>
/// Delete Language object. 
/// </summary>
/// <param name="LanguageId" >This is primary key to delete.</param>
/// <returns>True/False.</returns>

            public static bool Delete(string LanguageId)
    {
      String methodName = "DeleteLanguage";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteLanguage");
                //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(dbCommand, "LanguageId", DbType.AnsiString,LanguageId);        
         //Add value returnValue  paramater.
        ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
        try
        {
          ManagerDao.ExecuteNonQuery(dbCommand);        
          int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
          if (parameterValue == 1)
          {
            return true;
          }
        }
        catch (Exception)
        {
          throw;
        }
        return false;
      }
      catch (Exception e)
      {
        //write error message to log file.
        logger.Error(methodName, e);
        return false;
      }
    }  
           
	                }
                }